//
//  SongListAllViewController.m
//  YHYRealx
//
//  Created by yanghuiyan on 16/5/3.
//  Copyright © 2016年 yanghuiyan. All rights reserved.
//

#import "SongListAllViewController.h"

@interface SongListAllViewController ()<UITableViewDelegate, UITableViewDataSource>

@property (nonatomic, retain) YHYBaseTableView *tableView;

@property (nonatomic, retain) NSMutableArray * dataSource;

@property (nonatomic, retain) NSMutableArray *dataChildrenSource;

@end

@implementation SongListAllViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.view.backgroundColor = [UIColor colorWithRed:0.16 green:0.20 blue:0.25 alpha:1.00];
    UILabel *titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 200, 40)];
    titleLabel.text = @"歌单";
    titleLabel.font = [UIFont boldSystemFontOfSize:22];
    titleLabel.textColor = [UIColor grayColor];
    titleLabel.textAlignment = NSTextAlignmentCenter;
    self.navigationItem.titleView = titleLabel;
    [titleLabel release];
    
    [self createData];
    [self creatTableView];

    
}

- (void)creatTableView {
    self.tableView = [[YHYBaseTableView alloc] initWithFrame:CGRectMake(0, 0, WIDTH, HEIGHT - 64)];
    _tableView.delegate = self;
    _tableView.dataSource = self;
    [_tableView registerClass:[SongListAllTableViewCell class] forCellReuseIdentifier:@"SongListAllTableViewCellIdentifier"];
    [self.view addSubview:_tableView];
}

- (void)createData{
    
    self.dataSource = [NSMutableArray array];
    [YHYAFNetTool GETNetWirhUrl:@"http://api.dongting.com/song/tag/songlist" body:nil headerFile:nil response:YHYJSON Success:^(id result) {
        
        for (NSDictionary *dic in result[@"data"]) {
            SongListAllModel *songListTypeModel = [[SongListAllModel alloc] init];
            [songListTypeModel setValuesForKeysWithDictionary:dic];
            [_dataSource addObject:songListTypeModel];
        }
        [_tableView reloadData];
    } Failure:^(NSError *error) {
        
    }];
     
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    if (_dataSource.count) {
        return _dataSource.count + 1;
    }
    return 0;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return 1;
}
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
    if (section == 0) {
        return nil;
    }
    if (_dataSource) {
        SongListAllModel *songListTypeModel = _dataSource[section - 1];
        return songListTypeModel.tag;
    }
    return nil;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.section == 0) {
        UITableViewCell *cell = [[UITableViewCell alloc] init];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        YHYBaseLabel *label = [[YHYBaseLabel alloc] initWithFrame:CGRectMake(5, 0, WIDTH - 10, 40)];
        label.textAlignment = NSTextAlignmentCenter;
        label.text = @"全部";
        label.layer.borderWidth = 1.0f;
        label.font = [UIFont systemFontOfSize:23];
        [cell addSubview:label];
        return cell;
    }else {
        SongListAllTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"CPYSongListTypeTableViewCellIdentifier"];
        SongListAllModel *songListTypeModel = _dataSource[indexPath.section - 1];
        self.dataChildrenSource = [NSMutableArray array];
        for (NSDictionary *dic in songListTypeModel.children) {
            SongListAllChildrenModel *typeChildrenModel = [[SongListAllChildrenModel alloc] init];
            [typeChildrenModel setValuesForKeysWithDictionary:dic];
            [_dataChildrenSource addObject:typeChildrenModel];
        }
//        cell.delegate = self;
//        cell.dataTypeChildrenSource = _dataChildrenSource;
        return cell;
    }
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    switch (indexPath.section) {
        case 0:
            return 40;
            break;
        case 1:
            return 335;
            break;
        case 2:
            return 225;
            break;
        case 3:
            return 335;
            break;
        case 4:
            return 170;
            break;
        case 5:
            return 115;
            break;
        default:
            break;
    }
    return 0;
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
